<?php
/**
 * Created by JetBrains PhpStorm.
 * User: 蒋文书
 * Date: 14-12-27
 * Time: 下午11:27
 * To change this template use File | Settings | File Templates.
 */

namespace Admin\Controller;
use Think\Controller;
use Think\Upload;


class NewsController extends CommonController{

    /**
     * 新闻列表
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function indexNews(){
        //$this->navList=M('Along')->where(array('type'=>'News','lang'=>LANG_SET))->select();

              /* 查询条件初始化 */
        $map = array();
        $map  = array('type'=>1,'pid'=>1,'lang'=>LANG_SET);
        if(isset($_GET['pid'])){
            $map['pid']   =   I('pid',0);
        }
        $this->assign('news_id',I('get.pid',1));
        $this -> list = D('News')->where($map)->select();
        $this->meta_title = '新闻管理';
        $this->display();
    }
    /**
     * 新闻列表
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    /**public function newsType(){
        $doType=I('get.doType');
        $id = I('id',0,'intval');
        if($doType=='Add'){
            if(IS_POST){
              if(D('Along')->add()){
                  $this->success('新增成功！',U('newsType',array('Type'=>I('get.Type'),'doType'=>$doType)));
              }else{
                  $this->error('新增失败！');
              }
            }
        }else{           
            $this->assign('id',$id);
            $info = D('Along')->info($id);
            if(IS_POST){
              if(D('Along')->update($_POST)){
                  $this->success('新增成功！',U('newsType',array('Type'=>I('get.Type'),'doType'=>$doType)));
              }else{
                  $this->error('新增失败！');
              }
            }
        }
        $this->assign('doType',$doType); 

        $this -> list = M('along')->where(array('type'=>I('get.Type'),'lang'=>LANG_SET))->select();
        $this->assign('type',I('get.Type'));
        $this->meta_title = '新闻管理';
        $this->display();
    }**/

    /**
     * 新增新闻
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function addNews(){
        //$this->navList=M('Along')->where(array('type'=>'News','lang'=>LANG_SET))->select();
        if(IS_POST){
            //添加所属角色
            if(D('News')->addInfo()){
                 $this->success('新增成功！',U('IndexNews'));
            }else{
                $this->error('新增失败！');
            }
        }
        $this->assign('news_id',I('get.pid'));
        $this->meta_title = '新增新闻';
        $this->display();
    }

    /**
     * 删除新闻
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function delNews(){  
        $id =I('id',0,'intval');
        if(D('News')->del($id)){
            $this->success('删除成功',U('IndexNews'));
        }else{
            $this->error('删除失败！');
        }
    }

    /**
     * 编辑新闻
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function editNews(){
        $id = I('id',0,'intval');
        if(IS_POST){
           if(D('News')->update()){
                $this->success('更新成功',U('IndexNews'));
            }else{
                $this->error('更新失败！');
            }
        }else{
            $this -> info = D('News')->info($id);
        }
        $this -> meta_title = '编辑新闻';
        $this->display();
    }

    /**
     * 新增案例
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function addCase(){
        if(IS_POST){
            $setting = C('EDITOR_UPLOAD');
            $this -> uploader = new Upload($setting, 'LOCAL');
            $img   = $this->uploader->upload();
            $data = M('News')->create();    
            if($img){ 
                $data['pic']=$img['pic']['savepath'].$img['pic']['savename'];
                if(D('News')->addInfo($data)){
                     $this->success('新增成功！',U('IndexCase',array('pid'=>I('get.pid'))));
                }else{
                     $this->error('新增失败！');
                }
            }else{
               if(D('News')->addInfo($data)){
                     $this->success('新增成功！',U('IndexLinks'));
                }else{
                     $this->error('新增失败！');
                }
            }
        }
        $this->assign('news_id',I('get.pid'));
        $this->assign('type',I('get.pid'));
        $this->meta_title = '新增案例';
        $this->display();
    }

    /**
     * 案例列表
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function indexCase(){  
        //$this->navList=M('Along')->where(array('type'=>'Case','lang'=>LANG_SET))->select();
        /* 查询条件初始化 */
        $map = array();
        $map  = array('type'=>2,'pid'=>1,'lang'=>LANG_SET);
        if(isset($_GET['pid'])){
            $map['pid']   =   I('pid');
        }
        $this->assign('news_id',I('get.pid',1));
        $this -> list = D('News')->where($map)->select();
        $this->meta_title = '案例管理';
        $this->display();
    }

    /**
     * 编辑案例
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */

    public function editCase(){
        $id = I('id',0,'intval');
        if(IS_POST){
            $setting = C('EDITOR_UPLOAD');
            $this -> uploader = new Upload($setting, 'LOCAL');
            $img   = $this->uploader->upload();
            $data = M('News')->create(); 
            if($img){ 
                $data['pic']=$img['pic']['savepath'].$img['pic']['savename'];
                if(M('News')->save($data)){
                     $this->success('更新成功',U('IndexCase',array('pid'=>I('get.pid'))));
                }else{
                     $this->error('更新失败！');
                }
            }else{
               if(M('News')->save($data)){
                    $this->success('更新成功',U('IndexCase',array('pid'=>I('get.pid'))));
                }else{
                    $this->error('更新失败！');
                }
            }
        }else{
            $this -> info = D('News')->info($id);
        }
        $this -> meta_title = '编辑案例';
        $this->display();
    }

    /**
     * 删除案例
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function delCase(){  
        $id =I('id',0,'intval');
        if(D('News')->del($id)){
            $this->success('删除成功',U('IndexCase'));
        }else{
            $this->error('删除失败！');
        }
    }

   /**
     * 友情链接管理
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function indexLinks(){  
              /* 查询条件初始化 */
        $map  = array('type'=>3);
        $this -> list = D('News')->where($map)->select();
        $this->meta_title = '友情链接管理';
        $this->display();
    }

    /**
     * 新增友情链接
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function addLinks(){
        if(IS_POST){
            $setting = C('EDITOR_UPLOAD');
            $this -> uploader = new Upload($setting, 'LOCAL');
            $img   = $this->uploader->upload();
            $data = M('News')->create(); 
            if($img){
                $data['pic']=$img['pic']['savepath'].$img['pic']['savename'];
                if(D('News')->addInfo($data)){
                     $this->success('新增成功！',U('IndexLinks'));
                }else{
                     $this->error('新增失败！');
                }
            }else{
                if(D('News')->addInfo($data)){
                     $this->success('新增成功！',U('IndexLinks'));
                }else{
                     $this->error('新增失败！');
                }
            }
        }
        $this->meta_title = '新增友情链接';
        $this->display();
    }

    /**
     * 编辑友情链接
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */

    public function editLinks(){
        $id = I('id',0,'intval');
        if(IS_POST){
            $setting = C('EDITOR_UPLOAD');
            $this -> uploader = new Upload($setting, 'LOCAL');
            $img   = $this->uploader->upload();
            $data = M('News')->create(); 
            if($img){ 
                $data['pic']=$img['pic']['savepath'].$img['pic']['savename'];
                if(M('News')->save($data)){
                     $this->success('更新成功',U('IndexLinks'));
                }else{
                     $this->error('更新失败！');
                }
            }else{
               if(M('News')->save($data)){
                    $this->success('更新成功',U('IndexLinks'));
                }else{
                    $this->error('更新失败！');
                }
            }
        }else{
            $this -> info = D('News')->info($id);
        }
        $this -> meta_title = '编辑友情链接';
        $this->display();
    }

    /**
     * 删除案例
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function delLinks(){  
        $id =I('id',0,'intval');
        if(D('News')->del($id)){
            $this->success('删除成功',U('IndexLinks'));
        }else{
            $this->error('删除失败！');
        }
    }

    /**
     * ueditor
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function editor(){
        $setting = C('EDITOR_UPLOAD');
        $this -> uploader = new Upload($setting, 'LOCAL');
        $img   = $this->uploader->upload();
        $return = array();
        $return['url'] = $img['upfile']['savepath'].$img['upfile']['savename'];
        $title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
        $return['title'] = $title;
        $return['original'] = $img['upfile']['name'];
        $return['state'] = ($img)? 'SUCCESS' : $this->uploader->getError();
        $this->ajaxReturn($return);
    }
    
}